package com.cafuc.collections;

import com.sun.javafx.collections.MappingChange;

import java.util.*;

public class CollectionsOperation {
    public void listTest(){
        List<String> list1 = new ArrayList<>();
        list1.add("ddd");
        list1.add("aaa");
        list1.add("aaa");
        list1.add("bbb");
        //list1.add(null);
        list1.remove("aaa");
        list1.add(1, "eee");
        System.out.println(list1);

        List<Integer> list2 = Arrays.asList(2, 3, 4, 9, 8);
        System.out.println(list2);

        //循环
        for(int i = 0; i < list1.size(); i++){
            System.out.println(list1.get(i));
        }
        System.out.println("======================================");
        Iterator iterator = list1.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("======================================");
        for(String item : list1){
            System.out.println(item);
        }
        System.out.println("======================================");
        list1.stream().forEach(item->System.out.println(item));

        //排序
        list1.sort(Comparator.naturalOrder());
        System.out.println(list1);
        list1.sort(Comparator.reverseOrder());
        System.out.println(list1);
        list1.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        Collections.sort(list1, Comparator.reverseOrder());
        System.out.println(list1);

        Collections.shuffle(list1);
        System.out.println(list1);

        //list 与数组互转
        System.out.println(list1.toArray(new String[list1.size()]));
        //System.out.println(list1.toArray(String[] :: new));
        System.out.println(Arrays.asList(new String[]{"daxunhua", "hua","er"}));

    }

    public void setTest(){
        Set<String> set1 = new TreeSet<>();
        set1.add("aaa");
        set1.add("aaa");
      //  set1.add(null);
        set1.add("ddd");
        set1.add("bbb");
        System.out.println(set1);

        //循环
        for(String item : set1){
            System.out.println(item);
        }
    }

    public void mapTest(){
        Map<String, String> map1 = new HashMap<>();
        map1.put("aaa", "daxunhua");
        map1.put("bbb", "da");
        map1.put("ccc", "xun");
        map1.put("ccc", "ccc");
        map1.put("ddd", "null");
        System.out.println(map1);

        for(String key : map1.keySet()){
            System.out.println(map1.get(key));
        }
        for (Map.Entry<String, String> entry : map1.entrySet()){
            System.out.println(entry.getKey() + "--" + entry.getValue());
        }

    }
    public static void main(String[] args) {
        CollectionsOperation co = new CollectionsOperation();
        //co.listTest();
        co.setTest();
        co.mapTest();
    }
}
